import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator
text = open('C:/Users/Administrator/Desktop/sucai/alice.txt').read()
import imageio
mk = imageio.imread("C:/Users/Administrator/Desktop/sucai/alice_color.png")
wc = WordCloud(background_color="white",
mask=mk,)
wc.generate(text)
image_colors = ImageColorGenerator(mk)
fig, axes = plt.subplots(1, 3)
axes[0].imshow(wc)
axes[1].imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
# 右边的图片显示模板图片
axes[2].imshow(mk, cmap=plt.cm.gray)
for ax in axes:
    ax.set_axis_off()
plt.show()
# 给词云对象按模板图片的颜色重新上色
wc_color = wc.recolor(color_func=image_colors)
# 将词云图片导出到当前文件夹
wc_color.to_file('output10-alice.png')